<?php

namespace App\Http\Controllers\Backend\Flaw;

use App\Http\Controllers\Controller;
use App\Models\Flaw\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * index 漏洞分类列表展示
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request){
        $category = new Category;
        $categorys =  $request->type ? $category->getCategoryInfo(false):$category->getCategoryInfo();
        $type = $request->type? 1 : 0;
        return view('backend.flaw.catelist',['categorys'=>$categorys,'type'=>$type]);
    }

    /**
     * addCategory  增加栏目操作
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function  addCategory(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            $validator = Category::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/cateAdd')
                    ->withErrors($validator)
                    ->withInput();
            }
            Category::create($data);
            return redirect('/admin/cateIndex');
        }else{
            $category = new Category;
            $items = $category->getCategoryInfo();
            return view('backend.flaw.cateadd',['items'=>$items]);
        }
    }
    
    /**
     * update 更新栏目操作
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function update(Request $request){
        if($request->isMethod('post')){
            $validator = Category::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/cateUpdate/id/'.$request->id)
                    ->withErrors($validator)
                    ->withInput();
            }
            $save = Category::find($request->id);
            $pid = $request->pid == $save->id ?$save->pid:$request->pid;
            $save->pid = $pid;
            $save->name = $request->name;
            $save->title = $request->title;
            $save->keywords = $request->keywords;
            $save->description = $request->description;
            $save->save();
            return redirect('/admin/cateIndex');
        }else{
            $category = new Category;
            $items = $category->getCategoryInfo();
            $item =  Category::find($request->id);
            return view('backend.flaw.cateupdate',['item'=>$item,'items'=>$items]);
        }
    }

    /**
     * del 删除栏目操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function  del(Request $request){
        $childs = Category::find($request->id)->childCategory->isEmpty();
        if(!$childs){
            abort(500,'该栏目下面含有子级栏目，请先删除子级栏目！');
        }
        Category::where('id', $request->id)->delete();
        return redirect('/admin/cateIndex');
    }

    /**
     * restoreCategory  从回收站中恢复删除的数据
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restoreCate(Request $request){
        $getData = Category::onlyTrashed()->find($request->id);
        $count = Category::onlyTrashed()->where('id','=',$getData->pid)->count();
        if($count>0){
            abort(500,'请先恢复父级菜单，以便列表可以正常显示！');
        }
        Category::withTrashed()->where('id', $request->id)->restore();
        return back();
    }

    /**
     * forceDelCategory 物理删除，即直接从数据库中删除数据
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function forceDelCate(Request $request){
        $count = Category::withTrashed()->where('pid','=',$request->id)->count();
        if($count>0){
            abort(500,'该栏目下面含有子级栏目，请先删除子级栏目！');
        }
        Category::withTrashed()->where('id', $request->id)->forceDelete();
        return back();
    }
}
